using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class OptProduct : Page, IRequiresSessionState
{
    LogDB m_logdb = new LogDB();
    ProductDB m_productdb = new ProductDB();


    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }


    private void GetProductDeleteList(string modelNo, string revision, string modelName, string specification, string createDateBegin, string createDateEnd)
    {
        DataTable dt;
        string strDateFrom = "";
        string strDateTo = "";

        if (createDateBegin != "" && createDateEnd != "")
        {
            strDateFrom = createDateBegin + " 00:00:00";
            strDateTo = createDateEnd + " 23:59:59";
        }

        dt = m_productdb.getProductList(modelNo,
                                        revision,
                                        modelName,
                                        specification,
                                        strDateFrom,
                                        strDateTo, 1);

        gvDeleted.DataSource = dt;
        gvDeleted.DataBind();
    }


    private void GetProductActiveList(string modelNo, string revision, string modelName, string specification, string createDateBegin, string createDateEnd)
    {
        DataTable dt;
        string strDateFrom = "";
        string strDateTo = "";

        if (createDateBegin != "" && createDateEnd != "")
        {
            strDateFrom = createDateBegin + " 00:00:00";
            strDateTo = createDateEnd + " 23:59:59";
        }

        dt = m_productdb.getProductList(modelNo,
                                        revision,
                                        modelName,
                                        specification,
                                        strDateFrom,
                                        strDateTo, 0);

        gvActive.DataSource = dt;
        gvActive.DataBind();
    }


    private void GetProductList(string modelNo, string revision, string modelName, string specification, string createDateBegin, string createDateEnd)
    {
        if (SysUtil.IsAdministratorUsertype())
        {
            GetProductDeleteList(modelNo, revision, modelName, specification, createDateBegin, createDateEnd);
            GetProductActiveList(modelNo, revision, modelName, specification, createDateBegin, createDateEnd);
        }
        else
        {
            GetProductActiveList(modelNo, revision, modelName, specification, createDateBegin, createDateEnd);
            panelDeleted.Visible = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }
        if (!this.Page.IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.ProductManage.ToString() + " | " + ISitemap.ProductList.ToString();
            GetProductList("", "", "", "", "", "");
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetProductList(txtSearchModel.Text.Trim(),
                        "", txtSearchModelName.Text.Trim(),
                        txtSearchSpecification.Text.Trim(),
                        txtSearchCreateDateBegin.Text.Trim(),
                        txtSearchCreateDateEnd.Text.Trim());
    }


    protected void btnNewProduct_Click(object sender, EventArgs e)
    {
        base.Response.Redirect("OptProductAdd.aspx");
    }


    protected void gvActive_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvActive.PageIndex = e.NewPageIndex;
        GetProductList(txtSearchModel.Text.Trim(),
                        "", txtSearchModelName.Text.Trim(),
                        txtSearchSpecification.Text.Trim(),
                        txtSearchCreateDateBegin.Text.Trim(),
                        txtSearchCreateDateEnd.Text.Trim());
    }


    protected void gvDeleted_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDeleted.PageIndex = e.NewPageIndex;
        GetProductList(txtSearchModel.Text.Trim(),
                        "", txtSearchModelName.Text.Trim(),
                        txtSearchSpecification.Text.Trim(),
                        txtSearchCreateDateBegin.Text.Trim(),
                        txtSearchCreateDateEnd.Text.Trim());
    }


    protected void gvActive_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int ret = 0;

        if (e.CommandName == "halfDelete")
        {
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            ret = m_productdb.deleteProduct(gvActive.DataKeys[curRow.RowIndex].Values["Model"].ToString());
            if (ret == 0)
            {
                m_logdb.writeSucLog(SysUtil.m_functiong_product, "delete product succeed!");
                Jscript.Alert("Delete Succeed!", this.Page);
                GetProductList(txtSearchModel.Text.Trim(),
                                "", txtSearchModelName.Text.Trim(),
                                txtSearchSpecification.Text.Trim(),
                                txtSearchCreateDateBegin.Text.Trim(),
                                txtSearchCreateDateEnd.Text.Trim());
            }
            else
            {
                m_logdb.writeFaileLog(SysUtil.m_functiong_product, "delete product failed!");
                Jscript.Alert("Delete Failed!", this.Page);
            }
        }
    }


    protected void gvDeleted_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int ret = 0;
        if (e.CommandName == "Restore")
        {
            // do restore
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            ret = m_productdb.restoreProduct(gvDeleted.DataKeys[curRow.RowIndex].Values["Model"].ToString());
            if (ret == 0)
            {
                m_logdb.writeSucLog(SysUtil.m_functiong_product, "restore product succeed!");
                Jscript.Alert("Restore Succeed!", this.Page);
                GetProductList(txtSearchModel.Text.Trim(),
                                "", txtSearchModelName.Text.Trim(),
                                txtSearchSpecification.Text.Trim(),
                                txtSearchCreateDateBegin.Text.Trim(),
                                txtSearchCreateDateEnd.Text.Trim());
            }
            else
            {
                m_logdb.writeFaileLog(SysUtil.m_functiong_product, "restore product failed!");
                Jscript.Alert("Restore Failed!", this.Page);
            }
        }
        else if (e.CommandName == "ForceDelete")
        {
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            ret = m_productdb.trashProduct(gvDeleted.DataKeys[curRow.RowIndex].Values["Model"].ToString());
            if (ret == 0)
            {
                m_logdb.writeSucLog(SysUtil.m_functiong_product, "trash product succeed!");
                GetProductList(txtSearchModel.Text.Trim(),
                                "", txtSearchModelName.Text.Trim(),
                                txtSearchSpecification.Text.Trim(),
                                txtSearchCreateDateBegin.Text.Trim(),
                                txtSearchCreateDateEnd.Text.Trim());

                Jscript.Alert("Force Delete Succeed!", this.Page);
            }
            else
            {
                m_logdb.writeFaileLog(SysUtil.m_functiong_product, "trash product failed!");

                Jscript.Alert("Force Delete Failed!", this.Page);
            }
        }
    }
}
